package cn.ligoo.dtu.web.springmvc.platform;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import cn.ligoo.dtu.domain.UserDtu;
import cn.ligoo.dtu.domain.UserInfo;
import cn.ligoo.dtu.service.impl.FacadeImpl;
import cn.ligoo.dtu.web.Constants;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/platform/user-dtu")
public class UserDtuController {

	private final Logger logger = LoggerFactory.getLogger(UserDtuController.class);

	@Resource
	private MessageSource message;

	@Resource
	private FacadeImpl facade;

	public UserDtuController() {
		logger.debug("...constructor");
	}

	@RequestMapping(value = "/search", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String search(HttpServletRequest request, @RequestParam int page, @RequestParam int rows, UserDtu t) {
		logger.debug("...search");

		logger.debug("...page:{}, rows:{}", page, rows);
		logger.debug("...userDtu:{}", t.toString());

		int total = facade.getUserDtuService().getUserDtuCount(t);

		t.getRow().setOffset((page - 1) * rows);
		t.getRow().setLimit(rows);
		List<UserDtu> userDtuList = facade.getUserDtuService().getUserDtuList(t);

		String json = String.format(Constants.JSON_DATAGRID, JSON.toJSONString(userDtuList), total);

		logger.debug("...json:{}", json);
		return json;
	}

	@RequestMapping(value = "/json", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String json(HttpServletRequest request) {
		logger.debug("...json");

		UserDtu t = new UserDtu();
		t.setIs_del(Constants.BYTE_0);
		logger.debug("...userDtu:{}", t.toString());
		UserInfo userInfo = (UserInfo) WebUtils.getSessionAttribute(request, Constants.SESSION_USER_INFO);
		if (null != userInfo && null != userInfo.getIs_admin() && 1 != userInfo.getIs_admin()) {
			t.getMap().put("user_id", userInfo.getId()); // 如果不是管理员，传入user_id，取得此user_id对应的所有modem_id
			t.setUser_id(userInfo.getId());
		}
		List<UserDtu> userDtuList = facade.getUserDtuService().getUserDtuList(t);

		String json = JSON.toJSONString(userDtuList);
		logger.debug("...json:{}", json);
		return json;
	}

	@RequestMapping(value = "/delete", produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String delete(@RequestParam String[] ids) {
		logger.debug("...delete ids:{}", ids);
		UserDtu t = new UserDtu();
		t.getMap().put("__ids", ids);
		t.getMap().put("__isLogicalDelete", "true");

		int count = 0;
		try {
			count = facade.getUserDtuService().removeUserDtu(t);
		} catch (DataAccessException dae) {
			String msg = message.getMessage("errors.dae", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		}
		return String.format(Constants.JSON_SUCCESS, null, count);
	}

	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String save(UserDtu userDtu) {
		logger.debug("...save");
		logger.debug("userDtu:{}", userDtu.toString());

		userDtu.setCreate_time(new Date());
		try {
			if (null == userDtu.getId()) {
				facade.getUserDtuService().createUserDtu(userDtu);
			} else {
				facade.getUserDtuService().modifyUserDtu(userDtu);
			}
		} catch (DuplicateKeyException dke) {
			String msg = message.getMessage("errors.dae.DuplicateKeyException", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		} catch (DataAccessException dae) {
			String msg = message.getMessage("errors.dae", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		}
		return String.format(Constants.JSON_SUCCESS, null, null);
	}

	@RequestMapping(value = "/edit/{id}", produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String edit(@PathVariable Integer id) {
		logger.debug("...edit");
		UserDtu t = new UserDtu();
		t.setId(id);
		UserDtu userDtu = facade.getUserDtuService().getUserDtu(t);
		logger.debug("...userDtu:{}", userDtu);
		return JSON.toJSONString(userDtu);
	}
}
